require "Common/define"
require "Logic/Config"
require "Common/functions"

CostOpenManager = {};
local this = CostOpenManager;
local activeTableData;
local serverTableData;
local tagTableData;
local conditionFun = {};
local conditionNotOpenActive = {};
local activeList = {};
local circleActiveList = {};

function CostOpenManager.init()
    activeTableData = G.dataTable["t_storeentrance"];
    serverTableData = G.dataTable["t_storelist"];
    this.initCondition();
    --this.initActive();
end

function CostOpenManager.initCondition()
    table.insert(conditionFun, this.firstChargeOpen);
    table.insert(conditionFun, this.operationOpen);
    table.insert(conditionFun, this.openServiceOpen);
    table.insert(conditionFun, this.createRoleOpen);
    table.insert(conditionFun, this.consolidatedServerOpen);
    table.insert(conditionFun, this.achieveLevelOpen);
    table.insert(conditionFun, this.achieveCustomOpen);
    table.insert(conditionFun, this.achieveVipOpen);
    table.insert(conditionFun, this.achievePrepaidLimitOpen);
    table.insert(conditionFun, this.consumptionDiamondOpen);
end

function CostOpenManager.initActive()
    conditionNotOpenActive = {};
    activeList = {};
    circleActiveList = {};
    for key, value in pairs(serverTableData) do
        this.updataOneAvtiveStatus(value, conditionNotOpenActive, activeList, circleActiveList);
    end
    logicMgr.CostManager.resCostData(activeList, true);
end

function CostOpenManager.updateActive(id)
    if id == nil or conditionNotOpenActive[tostring(id)] == nil then
        return ;
    end
    local notOpenActive = {};
    local list = {};
    for key, value in pairs(conditionNotOpenActive[tostring(id)]) do
        this.updataOneAvtiveStatus(serverTableData[tostring(value)], notOpenActive, list, circleActiveList);
    end
    conditionNotOpenActive[tostring(id)] = notOpenActive;
    logicMgr.CostManager.resCostData(list, false);
end

function CostOpenManager.updateCircleActive()
    local list = {};
    for key, value in pairs(circleActiveList) do
        this.updataOneAvtiveStatus(serverTableData[tostring(value)], {}, list, {});
    end
    logicMgr.CostManager.resCostData(list, false);
end

function CostOpenManager.updataOneAvtiveStatus(value, notOpenActive, canShowActive, circleActive)
    function addActive(openTimeStatus)
        local data = {}
        data.marketID = value.f_StoreId;
        data.startTime = openTimeStatus;
        table.insert(canShowActive, data);
    end
    if value == nil then
        return ;
    end
    local openTimeStatus, condition= this.getActiveOpenTime(value);
    ---条件开启，未到条件
    if openTimeStatus == false then
        --local xx = {};
        --table.insert(xx, value.f_StoreId);
        --table.insert(xx, condition[2]);
        if notOpenActive[tostring(condition[1])] == nil then
            notOpenActive[tostring(condition[1])] = {};
        end
        table.insert(notOpenActive[tostring(condition[1])], value.f_StoreId);
    else
        local serverTime = HelperFunc.GetServerTime();
        if serverTime < openTimeStatus then
            serverTime = openTimeStatus;
        end
        if value.f_IsCircle == 1 then
            openTimeStatus = this.getOneCircleActiveOpenTime(value, serverTime);
            table.insert(circleActive, value.f_StoreId);
        elseif value.f_IsCircle == 2 then
            openTimeStatus = serverTime - (serverTime - openTimeStatus) % value.f_StoreIdPeriod;
            table.insert(circleActive, value.f_StoreId);
        end
        addActive(openTimeStatus);
    end
end

function CostOpenManager.getOneCircleActiveOpenTime(value, openTimeStatus)
    local openTime = logicMgr.CronMgr.GetNextDateTime(value.f_Circle, math.floor(openTimeStatus) - value.f_StoreIdPeriod);
    if openTime == "" then
        return 0;
    end
    return tonumber(openTime);
end

function CostOpenManager.getActiveOpenTime(value)
    local condition = decodeJsonStrToTabel(value.f_OpenCondition);
    if condition == nil then
        return false;
    end
    local max = 0;
    for k , v in pairs(condition) do
        local openTimeStatus = conditionFun[v[1]](value, v[2]);
        if openTimeStatus == false then
            return false, v;
        end
        max = math.max(max, openTimeStatus);
    end
    return max;
end

---1、首充后开启，参数填写为0
function CostOpenManager.firstChargeOpen(value, time)
    local startTime = logicMgr.CostManager.getFirstRechargeTime();
    if startTime == 0 then
        return false;
    else
        return startTime + time;
    end
end

---2、不开启的集市（给运营手动开启），参数为0
function CostOpenManager.operationOpen()
    return false;
end

---3、开服一定时间后开启，参数填写单位为秒
function CostOpenManager.openServiceOpen(value, time)
    local startTime = logicMgr.CostManager.getOpenServerTime() + time;
    return startTime;
end

---4、创角一定时间后开启，参数填写单位为秒
function CostOpenManager.createRoleOpen(value, time)
    local startTime = logicMgr.HeroManager.GetRoleInfo().createTime + time;
    return startTime;
end

---5、合服一定时间后开启，参数填写单位为秒
function CostOpenManager.consolidatedServerOpen(value, time)
    local startTime = logicMgr.CostManager.getCombineServerTime();
    if startTime == 0 then
        return false;
    else
        startTime = startTime + time
    end
    return startTime;
end

---6、玩家达到等级开启，参数填写等级
function CostOpenManager.achieveLevelOpen(value, num)
    local data = logicMgr.CostManager.getCostOpenServerConditionById(value.f_StoreId);
    return this.getTimeByDataAndId(data, 6, num);
end

---7、玩家通过指定关卡开启，参数填写关卡ID
function CostOpenManager.achieveCustomOpen(value, num)
    local data = logicMgr.CostManager.getCostOpenServerConditionById(value.f_StoreId);
    return this.getTimeByDataAndId(data, 7, num);
end

---8、玩家达到VIP等级开启，参数填写VIP等级
function CostOpenManager.achieveVipOpen(value, num)
    local data = logicMgr.CostManager.getCostOpenServerConditionById(value.f_StoreId);
    return this.getTimeByDataAndId(data, 8, num);
end

---9、玩家充值达到一定额度后开启，参数填写额度
function CostOpenManager.achievePrepaidLimitOpen(value, num)
    if logicMgr.CostManager.getRechargeSum() < num then
        return false;
    end
    local data = logicMgr.CostManager.getCostOpenServerConditionById(value.f_StoreId);
    return this.getTimeByDataAndId(data, 9, num);
end

---10、玩家消费钻石达到一定额度后开启，参数填写额度
function CostOpenManager.consumptionDiamondOpen(value, num)
    if logicMgr.CostManager.getCostGoldSum() < num then
        return false;
    end
    local data = logicMgr.CostManager.getCostOpenServerConditionById(value.f_StoreId);
    return this.getTimeByDataAndId(data, 10, num);
end

function CostOpenManager.getTimeByDataAndId(data, id, num)
    for k, v in pairs(data) do
        if v.conditionID == id then
            return tonumber(tostring(v.conditionParam)) * 0.001;
        end
    end
    return CostManager:GetLocalTimeById(id, num);
end

return CostOpenManager